package net.maku.utils;

import net.maku.framework.common.cache.RedisCache;
import net.maku.service.SysParamsCacheService;
import net.maku.service.TransCacheService;
import org.springframework.beans.BeansException;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Order(value = 1)
@Component
public class BeansUtils implements ApplicationRunner, ApplicationContextAware {

    private static ApplicationContext applicationContext;
    public static SysParamsCacheService SysParamsCache;
    public static RedisCache RedisCache;
    public static TransCacheService TransCacheService;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        try {
            SysParamsCache = applicationContext.getBean(SysParamsCacheService.class);
        } catch (Exception e) {
        }
        try {
            TransCacheService = applicationContext.getBean(TransCacheService.class);
        } catch (Exception e) {
        }
        try {
            RedisCache = applicationContext.getBean(RedisCache.class);
        } catch (Exception e) {
        }
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }


    public static Object getBean(String name) throws BeansException {
        return applicationContext.getBean(name);
    }
}
